//
//  LHHUtil.m
//  加密
//
//  Created by iOS Developer 2 on 13/01/15.
//  Copyright © 2015年 欢欢大帅哥. All rights reserved.
//

#import "LHHUtil.h"
#import "NSString+MD5Addition.h"
#import "UIDevice+Identifier_Addition.h"
#import "DeviceInformation.h"
#import "FMEncryption.h"
#define BUILDNUM        @"0001" //上线版build version
@interface LHHUtil(){
    FMEncryption* encryptImpl;
}
@end;
@implementation LHHUtil
singletonImplemention(LHHUtil)
- (NSString*) Encrypter:(NSString*)content
{
    if( encryptImpl == nil )
        encryptImpl = new FMEncryption();
    const char* datechar = [content UTF8String];
    
    char des[KCheckSumLength+1];
    memset(des, 0,sizeof(des));
    
    encryptImpl->Encipher(datechar,des,strlen(datechar));
    
    des[KCheckSumLength] = '\0';
//    DDLogWarn(@"%@",[[NSString alloc] initWithCString:des encoding:NSUTF8StringEncoding]);
    return [[NSString alloc] initWithCString:des encoding:NSUTF8StringEncoding];
}
- (NSString*) EncrypterData:(NSData*)data {
    
    if( encryptImpl == nil )
        encryptImpl = new FMEncryption();
    
    const char* datechar = (const char*)[data bytes];
//    DDLogDebug(@"%lu",sizeof(datechar));
//    DDLogError(@"%s",datechar);
    
    char des[KCheckSumLength+1];
    memset(des, 0,sizeof(des));
    
    encryptImpl->Encipher(datechar,des,[data length]);
    
    des[KCheckSumLength] = '\0';
//    DDLogWarn(@"%@",[[NSString alloc] initWithCString:des encoding:NSUTF8StringEncoding]);
    
    return [[NSString alloc] initWithCString:des encoding:NSUTF8StringEncoding];
}
// MARK: TEA加密算法
- (NSData*)EncodeTEA:(NSString*)string
{
    if( encryptImpl == nil )
        encryptImpl = new FMEncryption();
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    Byte *myByte = (Byte *)[data bytes];
    SignedByte *newByte = encryptImpl->EncodeTEA(myByte, (int)[data length]);
    
    return [NSData dataWithBytes:newByte length:[data length]];
}

// MARK: dataTEA
- (NSData*)dataEncodeTEA:(NSData*)data
{
    if( encryptImpl == nil )
        encryptImpl = new FMEncryption();
    Byte *myByte = (Byte *)[data bytes];
    SignedByte *newByte = encryptImpl->EncodeTEA(myByte, (int)[data length]);
    
    return [NSData dataWithBytes:newByte length:[data length]];
}

- (NSString*) getImei
{
    NSString *imei = [NSString stringWithFormat:@"%@;%@",[[UIDevice currentDevice] uniqueDeviceIdentifierr],[[UIDevice currentDevice] uniqueDeviceIdentifierrWithoutMd5]];
    // NSString *imei = [[UIDevice currentDevice] uniqueDeviceIdentifierr];
    //DDLogError(@"%@",imei);
    return imei;
}

- (NSString*) getUDID
{
    return [[UIDevice currentDevice] getBatteryID];
}

- (NSString*) getsDisplayMetrics
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    return [NSString stringWithFormat:@"%d*%d",(int)width,(int)height];
}

- (NSString*) getPhoneNumber
{
    NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];
    if( [num length ] )
        return num;
    return  @"";
}

- (NSString*)getSimOperator
{
    return @"";
}

- (NSString*) getModel
{
    return [DeviceInformation platform];
}

- (NSString*) getSystemRelease
{
    return [[UIDevice currentDevice] systemVersion];
}

- (NSString*) getVersionName
{
    NSString *versionName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    return [[versionName stringByReplacingOccurrencesOfString:@"." withString:@""] stringByAppendingString:BUILDNUM];
}
@end
